<?php
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.view');
class LinkViewLink extends JViewLegacy{
	
	public  $item = null;
	public function display($tpl = null) {
		
		$form = $this->get('Form');
		$item = $this->get('Item');
		
		$script = $this->get('Script');
		if (count($errors = $this->get('Errors'))) {
			JError::raiseError(500, implode('<br />', $errors));
			return false;
		}
		$this->form = $form;
		$this->item = $item;
		$this->script = $script;
		$this->addToolBar();
		parent::display($tpl);
		$this->setDocument();
	}
	protected function addToolBar() {
		JRequest::setVar('hidemainmenu', true);
		$user = JFactory::getUser();
		$userId = $user->id;
		$isNew = $this->item->id == 0;
		$canDo = LinkHelper::getActions($this->item->id);
		JToolBarHelper::title($isNew ? JText::_('Link Manager: New') : JText::_('Link Manager: Edit'), 'Link');
		// Built the actions for new and existing records.
		if ($isNew){
			
			if ($canDo->get('core.create')){
				
				JToolBarHelper::save('link.save');
				JToolBarHelper::custom('link.save2new', 'save-new.png', 'save-new_f2.png', 'JTOOLBAR_SAVE_AND_NEW', false);
			}
			JToolBarHelper::cancel('link.cancel');
		}else{
			if ($canDo->get('core.edit')){
				// We can save the new record
				JToolBarHelper::apply('link.apply');
				JToolBarHelper::save('link.save');
 
				// We can save this record, but check the create permission to see if we can return to make a new one.
				if ($canDo->get('core.create')) {
					JToolBarHelper::custom('link.save2new', 'save-new.png', 'save-new_f2.png', 'JTOOLBAR_SAVE_AND_NEW', false);
				}
			}
			if ($canDo->get('core.create')) {
				JToolBarHelper::custom('link.save2copy', 'save-copy.png', 'save-copy_f2.png', 'JTOOLBAR_SAVE_AS_COPY', false);
			}
			JToolBarHelper::cancel('link.cancel', 'JTOOLBAR_CLOSE');
		}
	}
	protected function setDocument() 
	{
		$isNew = $this->item->id == 0;
		$document = JFactory::getDocument();
		$document->setTitle($isNew ? JText::_('Link Creating') : JText::_('Link Editing'));
		$document->addScript(JURI::root() . $this->script);
		$document->addScript(JURI::root() . "/administrator/components/com_link/views/link/submitbutton.js");
		JText::script('COM_SLIDESHOW_SLIDESHOW_ERROR_UNACCEPTABLE');
	}
}
